package com.tjbaobao.videoplayol.ui;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.VideoView;

import com.tjbaobao.videoplayer.listener.OnTJVideoPlayerListener;
import com.tjbaobao.videoplayer.view.TJVideoView;
import com.tjbaobao.videoplayer.view.base.BaseProgressView;
import com.tjbaobao.videoplayol.R;

/**
 * Created by TJbaobao on 2017/8/10.
 */

public class TJVideoPlayerView extends TJVideoView {

    private ImageView iv_play_big ;
    private TextView tv_time;
    private OnTJVideoPlayerListener mOnTJVideoPlayerListener ;

    public TJVideoPlayerView(Context context) {
        super(context);
    }

    public TJVideoPlayerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TJVideoPlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 初始化组件
     */
    @Override
    protected void onInitView() {
        setOnTJListener(new MyOnTJVideoPlayerListener());
        iv_play_big = (ImageView) this.findViewById(R.id.iv_index);
        iv_play_big.setOnClickListener(this);
        tv_time = (TextView) this.findViewById(R.id.tv_time);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_index) {
            playPress();
        }
        super.onClick(v);
    }

    /**
     * 加载布局 必须
     * @return
     */
    @Override
    protected View onInflateLayout() {
        return inflate(this.getContext(),R.layout.video_play_view_layout,null);
    }

    /**
     * 加载播放按钮
     * @return
     */
    @Override
    protected ImageView onInitPlayView() {
        return (ImageView) this.findViewById(R.id.iv_play);
    }

    /**
     * 加载停止按钮(现在没有做这个功能)
     * @return
     */
    @Override
    protected ImageView onInitStopView() {
        return null;
    }

    /**
     * 加载视频组件
     * @return
     */
    @Override
    protected VideoView onInitVideoView() {
        return (VideoView) this.findViewById(R.id.videoView);
    }

    /**
     * 加载底部控制框
     * @return
     */
    @Override
    protected View onInitOperationColumnView() {
        return this.findViewById(R.id.ll_bottom);
    }

    /**
     * 加载 加载进度条
     * @return
     */
    @Override
    protected BaseProgressView onInitLoadingProgressView() {
        return (BaseProgressView) this.findViewById(R.id.pv_topLoad);
    }

    /**
     * 加载 播放进度条
     * @return
     */
    @Override
    protected BaseProgressView onInitPlayingProgressView() {
        return (BaseProgressView) this.findViewById(R.id.pv_progressBar);
    }

    //region =============================接口=================================

    private class MyOnTJVideoPlayerListener extends OnTJVideoPlayerListener
    {
        /**
         * 播放进度
         * @param currentPosition
         * @param duration
         */
        @Override
        public void onProgressPlay(int currentPosition, int duration) {
            tv_time.setText(toTimeStr(currentPosition)+"/"+toTimeStr(duration));
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onProgressPlay(currentPosition,duration);
            }
        }

        /**
         * 加载进度
         * @param progress
         */
        @Override
        public void onProgressLoad(float progress) {
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onProgressLoad(progress);
            }
        }

        /**
         * 播放
         */
        @Override
        public void onPlay() {
            ((ImageView)iv_play).setImageResource(R.drawable.app_videoplay_view_bt_pause);
            iv_play_big.setVisibility(INVISIBLE);
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onPlay();
            }
        }

        /**
         * 暂停
         */
        @Override
        public void onPause() {
            ((ImageView)iv_play).setImageResource(R.drawable.app_videoplay_view_bt_play);
            iv_play_big.setVisibility(VISIBLE);
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onPause();
            }
        }

        /**
         * 报错
         */
        @Override
        public void onError(MediaPlayer mp, int what, int extra, String msg) {
            Log.e("TJPlayer",msg);
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onError(mp,what,extra,msg);
            }
        }

        /**
         * 播放完成 proposal为true的时候是真正的完成的概率比较大，因为videoview有时候会莫名其妙的调用这个接口
         */
        @Override
        public void onCompletion(MediaPlayer mp, boolean proposal) {
            if(mOnTJVideoPlayerListener!=null)
            {
                mOnTJVideoPlayerListener.onCompletion(mp,proposal);
            }
        }

        @Override
        public void onPrepared(MediaPlayer mp) {
            if(mOnTJVideoPlayerListener!=null){
                mOnTJVideoPlayerListener.onPrepared(mp);
            }
        }
    }

    //endregion

    //region =============================内部方法=================================
    private String toTimeStr(long time)
    {
        int hous = (int) (time/1000/60/24);
        int min  = (int) (time/1000/60);
        int s = (int) (time/1000-min*60);
        String minStr = min>10?""+min :"0"+min;
        String sStr = s>10?""+s :"0"+s;
        return minStr+":"+sStr;
    }
    //endregion


    public void setOnTJVideoPlayerListener(OnTJVideoPlayerListener onTJVideoPlayerListener) {
        mOnTJVideoPlayerListener = onTJVideoPlayerListener;
    }
}
